package com.sicau.smarttraceplatform.controller;


import com.sicau.smarttraceplatform.common.constants.CommonURLConstants;
import com.sicau.smarttraceplatform.common.returning.ResultData;
import com.sicau.smarttraceplatform.entity.User;
import com.sicau.smarttraceplatform.entity.param.trace.TraceQueryH5Param;
import com.sicau.smarttraceplatform.entity.vo.PageDataVO;
import com.sicau.smarttraceplatform.entity.vo.goodtrace.GoodTraceVO;
import com.sicau.smarttraceplatform.entity.vo.trace.TraceInfoVO;
import com.sicau.smarttraceplatform.entity.vo.user.UserVO;
import com.sicau.smarttraceplatform.service.GoodTraceService;
import com.sicau.smarttraceplatform.service.TraceService;
import com.sicau.smarttraceplatform.service.UserService;
import com.sicau.smarttraceplatform.utils.QRCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * <p>
 * 农产品溯源信息 前端控制器
 * </p>
 *
 * @author ttxxi
 * @since 2020-06-03
 */
@RestController
@RequestMapping(CommonURLConstants.GOOD_TRACE_PREFIX+"/h5")
@Slf4j
@Api(tags = "H5页面商品溯源信息(扫描二维码查询溯源信息)")
public class GoodTraceController {


    @Resource
    private GoodTraceService goodTraceService;
    @Resource
    private TraceService traceService;
    @Resource
    private UserService userService;
    @Resource
    private Mapper dozerMapper;

    @GetMapping
    @ApiOperation("查询溯源信息")
    public ResultData getTraceInfo(@ApiParam(name = "traceId", value = "溯源id") @RequestParam String traceId) {
        GoodTraceVO goodTraceVO = goodTraceService.queryByTraceId(traceId);
        return new ResultData<>().OK().setObj(goodTraceVO);
    }

    @GetMapping("/trace")
    @ApiOperation(value = "查询耕种管收数据记录(传入条件则根据条件查询)")
    public ResultData<PageDataVO<TraceInfoVO>> getTrace(TraceQueryH5Param traceQueryH5Param) {
        log.info("查询耕种管收数据记录");
        return new ResultData<PageDataVO<TraceInfoVO>>().OK().setObj(traceService.queryTraceInfoH5ByCondition(traceQueryH5Param));
    }

    @GetMapping("/user")
    @ApiOperation("根据id查询用户信息")
    public ResultData getUser(@RequestParam String userId) {
        User user = userService.getById(userId);
        if (user!=null){
            UserVO userVO = dozerMapper.map(user, UserVO.class);
            return new ResultData<>().OK().setObj(userVO);
        }
        return new ResultData().FAILED();
    }

    /**
     * 网页图片前缀
     */
    private static final String IMG_PREFIX = "data:image/png;base64,";

    /**
     * 二维码网址前缀
     */
    @Value("${file.qrcode.prefix}")
    private String QRCODE_PREFIX;

    @GetMapping("/qrCode")
    @ApiOperation(value = "将二维码信息解析成base64图片资源字符串")
    public ResultData<String> parseQrCode(@ApiParam(name = "traceId", value = "溯源id") @RequestParam String traceId) {

        String content = QRCODE_PREFIX + traceId;
        String tmp = QRCodeUtil.creatRrCode(content, 200, 200);
        //避免换行符引起的图片字符串失效
        String code = IMG_PREFIX + tmp.replaceAll("\\r\\n", "");

        return new ResultData<String>().OK().setObj(code);
    }


}

